package com.mingyuan.library.domain.lending.book.model.entity;

import com.mingyuan.library.domain.common.model.valueobject.Version;
import com.mingyuan.library.domain.common.model.Entity;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookId;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookInformation;
import com.mingyuan.library.domain.lending.book.model.valueobject.BookType;
import com.mingyuan.library.domain.lending.librarybranch.model.valueobject.LibraryBranchId;
import com.mingyuan.library.domain.lending.patron.event.BookPlacedOnHold;
import com.mingyuan.library.domain.lending.patron.model.valueobject.PatronId;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Value;

/**
 * 可用图书信息
 */
@Value
@Entity
@AllArgsConstructor
@EqualsAndHashCode(of = "bookInformation")
public class AvailableBook implements Book {
    /**
     * 图书信息
     */
    BookInformation bookInformation;

    /**
     * 图书馆分馆ID
     */
    LibraryBranchId libraryBranch;
    /**
     * 版本号
     */
    Version version;

    public AvailableBook(BookId bookId, BookType type, LibraryBranchId libraryBranchId, Version version) {
        this(new BookInformation(bookId, type), libraryBranchId, version);
    }

    public boolean isRestricted() {
        return bookInformation.getBookType().equals(BookType.Restricted);
    }

    public BookId getBookId() {
        return bookInformation.getBookId();
    }

    /**
     * 搁置预定处理
     * @author yangzk
     * @param bookPlacedOnHold 预定中搁置图书
     * @date 2024/1/16 16:33
     **/
    public BookOnHold handle(BookPlacedOnHold bookPlacedOnHold) {
        return new BookOnHold(
                bookInformation,
                new LibraryBranchId(bookPlacedOnHold.getLibraryBranchId()),
                new PatronId(bookPlacedOnHold.getPatronId()),
                bookPlacedOnHold.getHoldTill(),
                version);
    }

}

